package team.star.werespos.config.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import team.star.werespos.exception.GlobalException;
import team.star.werespos.model.entity.User;
import team.star.werespos.model.enums.CodeMsgEnum;

/**
 * CurrPrincipal class<br/>
 *
 * @author hdonghong
 * @since 2019/05/02
 */
public class CurrPrincipal {

    public static User get() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (user == null) {
            throw GlobalException.create(CodeMsgEnum.LOG_IN);
        }
        return user;
    }

    public static void logout() {
        Subject subject = SecurityUtils.getSubject();
        // session 会销毁，在SessionListener监听session销毁，清理权限缓存
        if (subject.isAuthenticated()) {
            subject.logout();
        }
    }
}
